﻿/// <reference name="MicrosoftAjax.js"/>
/// <reference path="~/Clients/Scripts/jQuery/jquery-1.6.1-vsdoc.js"/>
/// <reference path="~/Clients/Scripts/globals.debug.js"/>
/// <reference path="~/Clients/Scripts/utils.debug.js"/>
/// <reference path="~/Clients/Scripts/errorHandling.debug.js"/>
/// <reference path="~/Clients/Scripts/jQuery/jquery.component.debug.js"/>

Type.registerNamespace("GreeNova");

GreeNova.JqueryLookup = function(element, selector, isComponent, cache) {
    Sys.Application.registerDisposableObject(this);

    this._element = element;
    this._selector = selector;
    this._isComponent = (isComponent === undefined ? false : isComponent);
    this._cache = (cache === undefined ? true : cache);
    this._jq;
    this._component;
};

GreeNova.JqueryLookup.prototype = {
    dispose: function() {
        this._element = null;
        this._selector = null;
        this._jq = null;
        this._component = null;
    },
    jq: function() {
        ///	<returns type="jQuery" />
        if (this._cache && this._jq)
            return this._jq;
        var element = this._element;
        if (GreeNova.JqueryLookup.isInstanceOfType(element))
            element = element.jq();
        else
            element = $(element);
        if (this._selector)
            element = element.find(this._selector);
        this._jq = element;
        return element;
    },
    component: function() {
        if (!this._isComponent) {
            debugger;
            throw "Not a component";
        }
        if (this._cache && this._component)
            return this._component;
        var component = this.jq().component();
        this._component = component;
        return component;
    },
    exists: function() {
        ///	<returns type="Boolean" />
        return this.jq().length > 0;
    }
};
GreeNova.JqueryLookup.registerClass('GreeNova.JqueryLookup', null, Sys.IDisposable);
